<?php
  /**
   * Kontrollbase
   *
   * An open source MySQL monitoring and analytics application
   *
   * @package Kontrollbase
   * @author Matt Reid
   * @copyright Copyright (c) 2009 Matt Reid, Kontrollbase LLC
   * @license http://kontrollsoft.com/kontrollbase/userguide/general-LICENSE.php
   * @link http://kontrollbase.com
   */

class Templates extends Controller {

  function Templates()
  {
    parent::Controller();
  }

  function index() {  
    $this->load->model('Model_templates', 'templates');
    log_message('debug', "templates_index function called");
   
    $g['data'] = $this->templates->get_data();
    $g['root'] = $this->config->item('base_url');

    $this->load->view('main/templates',$g);
  }  

  function submit() {
    log_message('debug', "templates_submit function called");
    $this->load->model('Model_templates', 'templates');
    $this->load->library('form_validation');  

    $action = $this->input->post("action");
    log_message('debug', "templates_submit: action = $action");

    if($action == "update") {
      log_message('debug', "templates_submit: action = update");

      $field = $this->input->post("field");
      $value = $this->input->post("value");
      $id = $this->input->post("id");

      $value = strtolower($value);
      log_message('debug', "Changing value of $field to lowercase.");
      log_message('debug', $_POST['id']);
      log_message('debug', "templates_submit: id: $id, field: $field, value: $value");
      
      $state = $this->templates->update_data($id,$field,$value);
      if($state == 0) {
        log_message('debug', "controller: JSON = {success: true}");
        echo "{success: true}";
      }
      elseif($state == 1) {
        log_message('debug', "failed: JSON = success: false, errors: { reason: 'Add failed. Please retry.' }}");
        echo "{success: false, errors: { reason: 'Add failed. Please retry.' }}";
      }
      else {
        show_error("This is a general failure message.");
      }
    }

    if($action == "insert") {
      log_message('debug', "templates_submit: action = insert");
      $state = $this->templates->add_data();
      if($state == 0) {
        log_message('debug', "controller: JSON = {success: true}");
        echo "{success: true}";
      }
      elseif($state == 1) {
        log_message('debug', "failed: JSON = success: false, errors: { reason: 'Insert failed. Please retry.' }}");
        echo "{success: false, errors: { reason: 'Add failed. Please retry.' }}";
      }
      else {
        show_error("This is a general failure message.");
      }
    }

    if($action == "delete") {
      log_message('debug', "templates_submit: action = delete");

      $this->form_validation->set_rules("id", "id", 'trim|required');
      $id = $this->input->post("id");
      log_message('debug', "templates_submit: delete2: id: $id");
      
      //we can't delete the defaul template!!
      if($id == '1') {
	log_message('debug', "Edit controller: subhostbinding - you cannot delete template id=1.");
	echo "{success: false, errors: { reason: 'You are not allowed to delete template id=1.'}}";
      }
      elseif ($this->form_validation->run() == FALSE) {    
        log_message('debug', "submit controller: form validation failed.");
        echo "{success: false, errors: { reason: 'Delete::Form validation failed. Please retry.'}}";
      }
      else {
        $state = $this->templates->delete_data($id);
        if($state == 0) {
          log_message('debug', "Submit controller: JSON = {success: true}");
          echo "{success: true}";
        }
        elseif($state == 1) {
          log_message('debug', "Submit failed: JSON = success: false, errors: { reason: 'Delete failed. Please retry.' }}");
          echo "{success: false, errors: { reason: 'Delete failed. Please retry.' }}";
        }
        else {
          show_error("This is a general failure message.");
        }
      }
    }

  }
}


?>